﻿Imports System.Xml
Imports System.Data.OleDb
Imports System.Web.Configuration
Imports System.Data
Partial Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'declare and instantiate connection object - connection string is stored in web.config
        Dim myConnection = New OleDbConnection()
        myConnection.ConnectionString = WebConfigurationManager.ConnectionStrings("yobboGuitar").ConnectionString
        'declare and instantiate command object
        Dim myCommand As New OleDbCommand("SELECT TOP 10 ID, title, bandName, genreName, dateAdded FROM songListing ORDER BY dateAdded;")
        myCommand.Connection = myConnection
        Dim myDataListing As New List(Of RSSData)
        Try
            myConnection.Open()
            Dim myReader As OleDbDataReader
            myReader = myCommand.ExecuteReader
            'read data into the collection of RSSData objects (see class definition below) - this collection is used for data binding
            Do While myReader.Read()
                myDataListing.Add(New RSSData(CType(myReader("title"), String), _
                                              CType(myReader("bandName"), String), _
                                              CType(myReader("genreName"), String), _
                                              CType(myReader("dateAdded"), String),
                                              CType(myReader("ID"), String)))
            Loop
            myConnection.Close()
        Catch ex As Exception

        Finally
            myConnection.Close()
        End Try
        'bind collection to repeater
        Me.repeater1.DataSource = myDataListing
        Me.DataBind()
    End Sub
End Class
''' <summary>
''' Class that has properties that correspond to the tags needed for the items in an RSS feed.  Used in a collection to facilitate data binding.
''' </summary>
''' <remarks></remarks>
Public Class RSSData
    Private f_title As Object
    Private f_description As Object
    Private f_link As Object
    Private f_pubdate As Object

    Public Property title As String
        Get
            Return f_title
        End Get
        Set(ByVal value As String)
            f_title = value
        End Set
    End Property
    Public Property description As String
        Get
            Return f_description
        End Get
        Set(ByVal value As String)
            f_description = value
        End Set
    End Property
    Public Property link As String
        Get
            Return f_link
        End Get
        Set(ByVal value As String)
            f_link = value
        End Set
    End Property
    Public Property pubdate As String
        Get
            Return f_pubdate
        End Get
        Set(ByVal value As String)
            f_pubdate = value
        End Set
    End Property
    ''' <summary>
    ''' Constructor
    ''' </summary>
    ''' <param name="title">Song title</param>
    ''' <param name="bandName">Band name</param>
    ''' <param name="genreName">Genre name</param>
    ''' <param name="dateAdded">Date added, as a string in RFC-822 format</param>
    ''' <param name="ID"></param>
    ''' <remarks></remarks>
    Public Sub New(ByVal title As String, ByVal bandName As String, ByVal genreName As String, ByVal dateAdded As String, ByVal ID As String)
        Me.title = convertReservedCharactersToXMLEntities(title)
        Me.description = convertReservedCharactersToXMLEntities("Artist: " & bandName & ", Genre: " & genreName)
        Me.link = "/yobboGuitar/displaySong.aspx?ID=" & ID
        Me.pubdate = convertReservedCharactersToXMLEntities(dateAdded)
    End Sub
    ''' <summary>
    ''' Function replaces reserved chars in XML with corresponding entities
    ''' </summary>
    ''' <param name="input"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function convertReservedCharactersToXMLEntities(ByVal input As String) As String
        input.Replace("&", "&0038;")
        input.Replace(">", "&0062;")
        input.Replace("<", "&0060;")
        input.Replace("""", "&0034;")
        input.Replace("'", "&0038;")
        Return input
    End Function
End Class